//
//  TwoViewController.m
//  music
//
//  Created by LT on 2019/3/6.
//  Copyright © 2019 liu. All rights reserved.
//

#import "TwoViewController.h"

#import <AFNetworking.h>
#import <YYWebImage.h>
#import "MoviesCell.h"
#import "MoviesModel.h"
@interface TwoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) YYAnimatedImageView *imageView;
@property (strong,nonatomic)UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *dys;
@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self httpGet];
}

-(UITableView *)tableView{
    if (_tableView==nil) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        
    }
    return _tableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 11;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str =@"cellStr";
    MoviesCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
    if (cell==nil) {
        cell=[[MoviesCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
        
    }
    cell.dyImageView.yy_imageURL=[NSURL URLWithString:self.moviesImageArr[indexPath.row]];
    cell.titleLabel.text=self.moviesNameArr[indexPath.row];
    cell.scoreLabel.text=self.moviesScoreArr[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}




-(void)httpGet{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    NSDictionary *dict = @{
    //                           @"username":@"jyq",
    //                           @"pwd":@"jyq",
    //                           @"type":@"JSON"
    //                           };
    //第一个参数:请求路径(NSString) (URL地址后面无需添加参数)
    //第二个参数:要发送给服务器的参数 (传NSDictionary)
    //第三个参数:progress 进度回调
    //第四个参数:success 成功的回调
    //responseObject:响应体(内部默认已经做了JSON的反序列处理)
    //    task.response:响应头信息
    //第五个参数:failure 失败的回调
    manager.responseSerializer=[AFJSONResponseSerializer serializer];
    [manager POST:@"https://api.douban.com/v2/movie/in_theaters?count=20" parameters:nil progress:nil success:
     ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         
         NSDictionary *dict=responseObject;
         //NSLog(@"%@",dict);
         NSArray *videoArr =[[NSArray alloc]initWithArray:dict[@"subjects"]];
         //NSLog(@"字典为%@",videoArr);
         //图片的获取
         self.moviesImageArr = [[NSMutableArray alloc]init];
         for (int i = 0; i < [videoArr count]; i++) {
             NSDictionary *dict = [[NSDictionary alloc]init];
             dict = videoArr[i];
             NSDictionary *imgDict = dict[@"images"];
             if(imgDict[@"large"] != NULL){
                 [self.moviesImageArr addObject:imgDict[@"large"]];
             }
         }
         NSLog(@"-----------------%@",self.moviesImageArr[0]);
         
         //电影名称的获取
         self.moviesNameArr=[[NSMutableArray alloc]init];
         for (int i=0; i<videoArr.count; i++) {
             NSDictionary *dict=[[NSDictionary alloc]init];
             dict = videoArr[i];
             NSDictionary *nameDict = dict[@"title"];
             [self.moviesNameArr addObject:nameDict];
         }
        NSLog(@"-----------------%@",self.moviesNameArr[0]);
         
         
         //评分
         self.moviesScoreArr = [[NSMutableArray alloc]init];
         for (int i = 0; i < [videoArr count]; i++) {
             NSDictionary *dict = [[NSDictionary alloc]init];
             dict = videoArr[i];
             NSDictionary *imgDict = dict[@"rating"];
             [self.moviesScoreArr addObject:imgDict[@"average"]];
         }
         
         NSLog(@"-----------------%@",self.moviesScoreArr[0]);
         //[self addSubview:self.dyImageView];
         //
         
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         
         NSLog(@"请求失败--%@",error);
     }];
    
}


- (NSMutableArray *)tgs
{
    if (!_dys) {
        NSDictionary *dict=@{
                             @"images":self.moviesImageArr[0],
                             @"title":self.moviesNameArr[0],
                             @"scrore":self.moviesScoreArr[0],
                             @"introduce":self.moviesIntroduceArr[0],
                             };
        
        [_dys addObject:[MoviesModel dyWithDict:dict]];
        
        
    }
    return _dys;
}

@end
